//
//  PwdViewController.m
//  ChatDemo-UI2.0
//
//  Created by romens on 15/7/2.
//  Copyright (c) 2015年 romens. All rights reserved.
//

#import "PwdViewController.h"
#import "ToolHelper.h"
#import "WebRequestData.h"
#import "RuntimeStatus.h"
#import "AppDelegate.h"
@interface PwdViewController ()

@property (strong, nonatomic) IBOutlet UITextField *OLdPwdTextField;
@property (strong, nonatomic) IBOutlet UITextField *NewPwdTextField;
@property (strong, nonatomic) IBOutlet UITextField *CheckPwdTextField;
@property (strong, nonatomic) IBOutlet UIButton *loginButton;
@property (strong, nonatomic) IBOutlet UIButton *backButton;

- (IBAction)doLogin:(id)sender;


@end

@implementation PwdViewController
@synthesize OLdPwdTextField = _OLdPwdTextField;
@synthesize NewPwdTextField = _NewPwdTextField;
@synthesize CheckPwdTextField = _CheckPwdTextField;
@synthesize loginButton = _loginButton;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupForDismissKeyboard];
    self.IsShowUserInfo=NO;
     self.title=@"密码更改";
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = leftButton;
    self.loginButton.backgroundColor=SystemColor;
    self.backButton.backgroundColor=SystemColor;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doLogin:(id)sender
{
    if([self empty])
    {
        NSString*oldpassword=_OLdPwdTextField.text;
        NSString*password=_CheckPwdTextField.text;
        NSString* newpassword=  [ToolHelper md5forX:[[ToolHelper md5forX:password] stringByAppendingString:@"0"]];
       
//        NSString *HXPwd=[ToolHelper md5forX:[[ToolHelper md5forX:password] stringByAppendingString:@"0"]];
//        NSString *newpassword=[ToolHelper md5forX:HXPwd];
        
        NSMutableDictionary *datass= [NSMutableDictionary new];
        [datass setValue:SystemPhoneNumber forKey:@"PHONE"];
        [datass setValue:oldpassword forKey:@"OLDPWD"];
        [datass setValue:newpassword forKey:@"NEWPWD"];
        [datass setValue:SystemLOGINNAME forKey:@"USERNAME"];
        [datass setValue:SystemOrgGuid forKey:@"ORGGUID"];
        [self Request:[SystemUrl stringByAppendingString:@"Handle"] queryType:@"Changepwd" postData:datass success:^(id data){
            WebRequestData*data1=(WebRequestData*)data;
             if(data1.Error.length>0)
             {
                [self SHowMessage:data1.Error type:showError];
             }
            else
            {
                [self showHint:@"密码修改成功"];
                //保存新密码；
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:password forKey:@"userpwd"];
                __weak AppDelegate * app=[UIApplication sharedApplication].delegate;
                [app Login];
            }
        } showHud:YES];

    }
}




-(BOOL)empty
{
    NSString*password=_OLdPwdTextField.text;
    NSString *newpassword= [ToolHelper md5forX:[[ToolHelper md5forX:password] stringByAppendingString:@"0"]];
    NSString*OldPwd=SystemUserPWD;
    if(![newpassword isEqualToString:OldPwd])
    {
        [self SHowMessage:@"旧密码输入不正确" type:showError];
        return  NO;
    }
    if(_CheckPwdTextField.text.length<6)
    {
        [self SHowMessage:@"密码至少6位" type:showError];
        return  NO;
    }
    if(_OLdPwdTextField.text.length==0||_CheckPwdTextField.text.length==0)
    {
        [self SHowMessage:@"密码不能为空" type:showError];
        return  NO;
    }
    if(![_NewPwdTextField.text isEqualToString: _CheckPwdTextField.text])
    {
        [self SHowMessage:@"新密码与确认密码不一致" type:showError];
        return  NO;
    }
    return  YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)btnBackClick:(id)sender {
  [self.navigationController popViewControllerAnimated:YES];
}

@end
